package com.ping.study.service.impl;

import com.ping.study.mapper.UrlsMapper;
import com.ping.study.model.dto.addUrls;
import com.ping.study.model.vo.live.LiveUrl;
import com.ping.study.service.UrlsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class UrlsServiceImpl implements UrlsService {

    @Autowired
    private UrlsMapper urlsMapper;
    @Override
    public List<HashMap<String, List<LiveUrl>>> getUrls() {
        List<HashMap<String,List<LiveUrl>>> urlsList = new ArrayList<>();
        List<String> gameIds = urlsMapper.selectGameIds();
        gameIds.forEach(gameId -> {
            List<LiveUrl> maps = urlsMapper.selectUrlsListByGameId(gameId);
            HashMap<String, List<LiveUrl>> map = new HashMap<>();
            map.put(gameId,maps);
            urlsList.add(map);
        });  // 添加右括号和分号
        return urlsList;
    }

    @Override
    public void addUrls(addUrls addUrls) {
        urlsMapper.insertUrlsWithGameId(addUrls.getGameId(), addUrls.getUrls());
    }

    @Override
    public void deleteUrlById(Integer id) {
        try {
            urlsMapper.deleteUrlById(id);
        }
        catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void deleteAllUrls() {
        urlsMapper.deleteAllUrls();
    }

}
